package io.renren.modules.workmanagement.controller;

import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.modules.sys.controller.AbstractController;
import io.renren.modules.workmanagement.constans.UserConstans;
import io.renren.modules.workmanagement.entity.QuestionBankEntity;
import io.renren.modules.workmanagement.service.QuestionBankService;
import io.renren.modules.workmanagement.vo.QuestionBankVO;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;



/**
 * 题库
 *
 * @author yewei
 * @email sunlightcs@gmail.com
 * @date 2022-04-17 16:15:30
 */
@RestController
@RequestMapping("workmanagement/questionbank")
public class QuestionBankController extends AbstractController {
    @Autowired
    private QuestionBankService questionBankService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("teacher:sys:list")
    public R list(@RequestParam Map<String, Object> params){
        Long userId = getUserId();
        params.put(UserConstans.USER_ID_KEY, userId);
        PageUtils page = questionBankService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("teacher:sys:info")
    public R info(@PathVariable("id") Long id){
        QuestionBankVO questionBankVO = questionBankService.getByBankId(id);
        return R.ok().put("questionBank", questionBankVO);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("teacher:sys:save")
    public R save(@RequestBody QuestionBankVO questionBank){
        questionBankService.saveQuestionBank(questionBank,getUserId());
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("teacher:sys:update")
    public R update(@RequestBody QuestionBankVO questionBank){
		questionBankService.updateQuestionBank(questionBank);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("teacher:sys:delete")
    public R delete(@RequestBody String[] ids){
		questionBankService.removeQuestionBanks(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 信息
     */
    @RequestMapping("/getInfo")
    @RequiresPermissions("teacher:sys:info")
    public R getInfo(@RequestParam(required = false) Long courseId){
        List<QuestionBankEntity> questionBanks = questionBankService.getListByClassCourseIdAndCreateUser(courseId,getUserId());
        return R.ok().put("questionBanks", questionBanks);
    }
}
